package com.mingmen.supplier.activity;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.supplier.R;
import com.mingmen.supplier.adapter.BohuiyuanAdapter;
import com.mingmen.supplier.defineview.GlideRoundTransform;
import com.mingmen.supplier.httpconfig.AppConfig;
import com.mingmen.supplier.jingli.JingLiRuRightAdapter;
import com.mingmen.supplier.model.JingLiRukuModel;
import com.mingmen.supplier.model.NoticeDetailModel;
import com.mingmen.supplier.model.NoticeModel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yjwfn on 17-9-23.
 */

public class NoticeDetailAdapter extends RecyclerView.Adapter<NoticeDetailAdapter.PhotoViewHolder> {


    Context context;
    List<NoticeModel.DataBean> myList = new ArrayList<>();

    private OnitemClick onitemClick;   //定义点击事件接口

    public NoticeDetailAdapter(Context context, List<NoticeModel.DataBean> myList) {
        this.context = context;
        this.myList = myList;
    }

    public void setDataList(List<NoticeModel.DataBean> myList) {
        this.myList = myList;

        notifyDataSetChanged();

    }

    //定义设置点击事件监听的方法
    public void setOnitemClickLintener(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        return new PhotoViewHolder(inflater.inflate(R.layout.item_noticedetail, parent, false));
    }

    @Override
    public void onBindViewHolder(PhotoViewHolder holder, int position) {
//        holder.photoView.setImageResource(photos[position]);
//        NoticeDetailModel.DataBean dataBean = myList.get(position);
        NoticeModel.DataBean dataBean = myList.get(position);
        Glide.with(context).load(AppConfig.IMGURL + dataBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(context, 5)).into(holder.ivAvatar);
        holder.text_name.setText(myList.get(position).getTitle());
        holder.content.setText(dataBean.getContent());
        Date now = new Date();
// 把Date按照格式转换成字符串
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdf.format(now));
// 把字符串按照格式转换成Date对象
        try {
            Date date1 = sdf.parse(dataBean.getCreateDate());
            String date = sdf.format(date1);//格式化数据bai
            holder.createDate.setText(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (onitemClick != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //在TextView的地方进行监听点击事件，并且实现接口
                    onitemClick.onItemClick(v, myList, position);
                }
            });
        }
        holder.text_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onitemClick.onItemClick(v, myList, position);
            }
        });
        holder.text_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onitemClick.onItemClick(v, myList, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return myList.size();
    }

    static final class PhotoViewHolder extends RecyclerView.ViewHolder {


        TextView text_name;
        TextView text_left;
        TextView text_right;
        ImageView ivAvatar;
        TextView content;
        TextView createDate;

        public PhotoViewHolder(View itemView) {
            super(itemView);

            text_name = itemView.findViewById(R.id.text_name);
            text_left = itemView.findViewById(R.id.text_left);
            text_right = itemView.findViewById(R.id.text_right);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            content = itemView.findViewById(R.id.content);
            createDate= itemView.findViewById(R.id.createDate);
        }
    }

    //定义一个点击事件的接口
    public interface OnitemClick {
        void onItemClick(View view, List<NoticeModel.DataBean> dataList, int position);
    }
}
